#include #include #include using std::ifstream; using std::ostream; using std::ios_base; using std::string; using std::cout; using std::cin; using std::endl; struct BitmapFileHeader { char header[2]; unsigned int file_size; unsigned short reserved_1; unsigned short reserved_2; unsigned int image_start; }; struct BitmapInfoHeader { unsigned int size; unsigned int bitmap_width; unsigned int bitmap_height; unsigned short color_planes; unsigned short bits_per_pixel; unsigned int compression_method; unsigned int raw_bitmap_size; unsigned int horizontal_resolution; unsigned int vertical_resolution; unsigned int color_palette_size; unsigned int important_colors_used; }; struct Pixel { unsigned char blue; unsigned char green; unsigned char red; }; struct Image { BitmapFileHeader bitmap_file_header; BitmapInfoHeader bitmap_info_header; }; ostream& operator<<(ostream&, const Image&); ostream& operator<<(ostream&, const BitmapFileHeader&); ostream& operator<<(ostream&, const BitmapInfoHeader&); int main() { Image image; ifstream bitmap; string bitmap_filename; cout << "Bitmap to read? "; getline(cin, bitmap_filename); bitmap.open(bitmap_filename, ios_base::binary); bitmap.read(image.bitmap_file_header.header, 2); bitmap.read((char*)&image.bitmap_file_header.file_size, 4); bitmap.read((char*)&image.bitmap_file_header.reserved_1, 2); bitmap.read((char*)&image.bitmap_file_header.reserved_2, 2); bitmap.read((char*)&image.bitmap_file_header.image_start, 4); bitmap.read((char*)&image.bitmap_info_header.size, 4); bitmap.read((char*)&image.bitmap_info_header.bitmap_width, 4); bitmap.read((char*)&image.bitmap_info_header.bitmap_height, 4); bitmap.read((char*)&image.bitmap_info_header.color_planes, 2); bitmap.read((char*)&image.bitmap_info_header.bits_per_pixel, 2); bitmap.read((char*)&image.bitmap_info_header.compression_method, 4); bitmap.read((char*)&image.bitmap_info_header.raw_bitmap_size, 4); bitmap.read((char*)&image.bitmap_info_header.horizontal_resolution, 4); bitmap.read((char*)&image.bitmap_info_header.vertical_resolution, 4); bitmap.read((char*)&image.bitmap_info_header.color_palette_size, 4); bitmap.read((char*)&image.bitmap_info_header.important_colors_used, 4); unsigned char blue; unsigned char green; unsigned char red; for(int row = 0; row < image.bitmap_info_header.bitmap_height; row++) { for(int column = 0; column < image.bitmap_info_header.bitmap_width; column++) { bitmap.read((char*)&blue, 1); bitmap.read((char*)&green, 1); bitmap.read((char*)&red, 1); cout << (int)blue << ":" << (int)green << ":" << (int)red << endl; } bitmap.read((char*)&red, 1); bitmap.read((char*)&red, 1); bitmap.read((char*)&red, 1); } bitmap.close(); cout << image; } ostream& operator<<(ostream& out, const Image& image) { out << image.bitmap_file_header << endl; out << image.bitmap_info_header << endl; return out; } ostream& operator<<(ostream& out, const BitmapFileHeader& file_header) { out << "Bitmap file header: \t" << endl; out << "File header: \t\t " << (char*)file_header.header << endl; out << "File size: \t\t " << file_header.file_size << endl; out << "Reserved: \t\t " << file_header.reserved_1 << endl; out << "Reserved: \t\t " << file_header.reserved_2 << endl; out << "Image start: \t\t " << file_header.image_start << endl; return out; } ostream& operator<<(ostream& out, const BitmapInfoHeader& info_header) { out << "Bitmap information header: " << endl; out << "File header: \t\t " << info_header.size << endl; out << "Bitmap width: \t\t " << info_header.bitmap_width << endl; out << "Bitmap height: \t\t " << info_header.bitmap_height << endl; out << "Color planes: \t\t " << info_header.color_planes << endl; out << "Bits/pixel: \t\t " << info_header.bits_per_pixel << endl; out << "Compression method: \t " << info_header.compression_method << endl; out << "Raw bitmap size: \t " << info_header.raw_bitmap_size << endl; out << "Resolution (horizontal): " << info_header.horizontal_resolution << endl; out << "Resolution (horizontal): " << info_header.vertical_resolution << endl; out << "Color palette size: \t " << info_header.color_palette_size << endl; out << "Important colors: \t " << info_header.important_colors_used << endl; return out; }